package com.zenithmind.news.controller;

import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import com.zenithmind.news.pojo.vo.NewsVO;
import com.zenithmind.news.service.NewsQueryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新闻查询控制器 - 遵循单一职责原则
 * 专门负责新闻的各种查询操作
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "新闻查询", description = "新闻查询相关接口")
@RestController
@RequestMapping("/api/zenithMind/v1/news/query")
@RequiredArgsConstructor
public class NewsQueryController {

    private final NewsQueryService newsQueryService;

    @Operation(summary = "查询热门新闻", description = "查询热门新闻列表")
    @GetMapping("/hot")
    public Result<List<NewsVO>> getHotNews(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<NewsVO> news = newsQueryService.getHotNews(limit);
        return Result.success(news);
    }

    @Operation(summary = "查询推荐新闻", description = "查询个性化推荐新闻")
    @GetMapping("/recommend")
    public Result<List<NewsVO>> getRecommendNews(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        String userId = UserContext.getUserId();
        List<NewsVO> news = newsQueryService.getRecommendNews(userId, limit);
        return Result.success(news);
    }

    @Operation(summary = "查询置顶新闻", description = "查询置顶新闻列表")
    @GetMapping("/top")
    public Result<List<NewsVO>> getTopNews(
            @Parameter(description = "分类ID") @RequestParam(required = false) String categoryId,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "5") Integer limit) {
        List<NewsVO> news = newsQueryService.getTopNews(categoryId, limit);
        return Result.success(news);
    }

    @Operation(summary = "查询相关新闻", description = "查询相关新闻列表")
    @GetMapping("/related/{id}")
    public Result<List<NewsVO>> getRelatedNews(
            @Parameter(description = "新闻ID") @PathVariable String id,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "5") Integer limit) {
        List<NewsVO> news = newsQueryService.getRelatedNews(id, limit);
        return Result.success(news);
    }

    @Operation(summary = "根据标签查询新闻", description = "根据标签查询新闻列表")
    @GetMapping("/tags")
    public Result<List<NewsVO>> getNewsByTags(
            @Parameter(description = "标签列表") @RequestParam List<String> tags,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "10") Integer limit) {
        List<NewsVO> news = newsQueryService.getNewsByTags(tags, limit);
        return Result.success(news);
    }

    @Operation(summary = "搜索新闻", description = "根据关键词搜索新闻")
    @GetMapping("/search")
    public Result<List<NewsVO>> searchNews(
            @Parameter(description = "关键词") @RequestParam String keyword,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<NewsVO> news = newsQueryService.searchNews(keyword, limit);
        return Result.success(news);
    }
}
